import path from "path";

import { createDefineMock } from "vite-plugin-mock-dev-server";

const defineMock = createDefineMock((mock) => {
  mock.url = path.join(import.meta.env.VITE_APP_BASE_API, mock.url);
});

const model_list: any = [];

const count = 100;

for (let i = 0; i < count; i++) {
  model_list.push({
    id: i + 1,
    modelName: `模型${i + 1}`,
    modelDesc: `模型${i + 1}的描述`,
    modelAuthor: `作者${i + 1}`,
    modelVersion: `v1.0.0`,
    createTime: new Date().toLocaleString(),
    isDeleted: 0,
    modelCall: ["all", "rpc", "http"][Math.floor(Math.random() * 3)],
    modelUrl: `/api/v1/model/${i + 1}`,
    status: [0, 1][Math.floor(Math.random() * 2)],
  });
}

export default defineMock([
  {
    url: "/api/v1/model/list",
    method: ["GET"],
    body: ({ query }) => {
      const { type, title, page = 1, limit = 10 } = query;
      const mock_list = model_list.filter((item: any) => {
        if (type && item.type !== type) return false;
        if (title && item.title.indexOf(title) < 0) return false;
        if (item.status === "deleted") return false;
        return true;
      });
      const page_list = mock_list.filter(
        (item: any, index: number) =>
          index < limit * page && index >= limit * (page - 1)
      );

      return {
        code: 200,
        data: { total: mock_list.length, page: page, records: page_list },
        msg: "一切ok",
      };
    },
  },

  {
    url: "/api/v1/model/info",
    method: ["GET"],
    body: ({ query }) => {
      console.log("request", query);
      return {
        code: 200,
        data: {},
        msg: "一切ok",
      };
    },
  },
]);
